package com.guigu.qingcheng.systemservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.system.TbResource;
import com.guigu.qingcheng.systemservice.mapper.TbResourceMapper;
import com.guigu.qingcheng.systemservice.service.TbResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbResourceServiceImpl extends ServiceImpl<TbResourceMapper, TbResource> implements TbResourceService {
    @Autowired
    private TbResourceService service;

    /**
     * 条件查询
     * @param searchMap， 查询所带条件
     * @return
     */
    @Override
    public List<TbResource> findList(Map<String, Object> searchMap) {
        QueryWrapper<TbResource> wapper = queryWrapper(searchMap);
        List<TbResource> list = service.list(wapper);
        return list;
    }

    /**
     * 分页条件查询
     * @param page 页号
     * @param size 页大小
     * @param searchMap 查询所带条件
     * @return
     */
    @Override
    public Page<TbResource> findPage(Integer page, Integer size, Map<String, Object> searchMap) {
        Page<TbResource> pg = new Page<TbResource>(page, size);
        QueryWrapper<TbResource> wapper = queryWrapper(searchMap);
        IPage<TbResource> list = service.page(pg, wapper);
        return pg;
    }


    /**
     * 高级组合查询封装对象
     * @param searchMap
     * @return
     */
    public QueryWrapper<TbResource> queryWrapper(Map<String, Object> searchMap){
        QueryWrapper<TbResource> wapper = new QueryWrapper<TbResource>();
        if(searchMap != null){
            if(searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                wapper.eq("id", searchMap.get("id"));
            if(searchMap.get("resKey") != null && !"".equals(searchMap.get("resKey")))
                wapper.like("res_key", searchMap.get("resKey"));
            if(searchMap.get("resName") != null && !"".equals(searchMap.get("resName")))
                wapper.like("res_name", searchMap.get("resName"));
            if(searchMap.get("parentId") != null && !"".equals(searchMap.get("parentId")))
                wapper.eq("parent_id", searchMap.get("parentId"));

        }
        return wapper;
    }
    @Override
    public List<Map> findAllMenu() {
        List<TbResource> menuList = service.findList(null);
        return findMenuListByParentId(menuList, 0);
    }

    private List<Map> findMenuListByParentId(List<TbResource> menuList, Integer parentId){
        List<Map> mapList = new ArrayList<>();
        for( TbResource r: menuList ){
            if(r.getParentId() == parentId){
                Map map = new HashMap();
                map.put("id",r.getId());
                map.put("label",r.getResName());
                map.put("children", findMenuListByParentId(menuList, r.getId()));
                mapList.add(map);
            }
        }
        return mapList;
    }

}
